<?php


namespace Hlcreation\Basic;

use Intervention\Image\Facades\Image;

class PictureMerging
{
    public function merageImg($source_img = []){
        $bg = $_SERVER['DOCUMENT_ROOT']."/".$source_img['bg_path'];
        //创建图片的实例
        list($_w,$_h) = getimagesize($bg);
        if ($_w > 800) {
            // 缩放
            $img = Image::make($source_img['bg_path'])->resize(800, null, function ($constraint) {
                $constraint->aspectRatio();
            });
            $img->save($source_img['bg_path']);
        }
        $bg_img = imagecreatefrompng(($bg)); // 背景图片

        unlink($bg);

        $x1 = $source_img['x'] + 10;
        $y1 = $source_img['y'] + 10;

        $d_path = $_SERVER['DOCUMENT_ROOT']."/".$source_img['path'];
        list($_dw,$_dh) = getimagesize($d_path);

        $d_src = imagecreatefrompng($d_path);

        $d_new = imagecreate($source_img['w'], $source_img['h']);
        imagecopyresized($d_new, $d_src, 0,0, 0,0, $source_img['w'], $source_img['h'], $_dw, $_dh);

        imagecopy($bg_img, $d_new, $x1, $y1, 0,0, $source_img['w'], $source_img['h']);
        imagedestroy($d_new);


        /**
         *  处理透明区域防止变为黑色背景
         */
        imagealphablending($bg_img,false);
        imagesavealpha($bg_img,true);



        imagepng($bg_img, $bg);
        imagedestroy($bg_img);
        return $bg;
    }
}
